package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author 杨林
 * @create 2022-09-14 23:16 星期三
 * description:
 */
@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {

    @Reference
    private AdminService adminService;

    @PreAuthorize("hasAnyAuthority('admin.show')")
    @RequestMapping
    public String index(HttpServletRequest request, ModelMap model){

        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);

        model.addAttribute("page",page);
        model.addAttribute("filters",filters);

        return "admin/index";
    }

    @PreAuthorize("hasAnyAuthority('admin.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){

        adminService.delete(id);

        return "redirect:/admin";
    }


    @Autowired
    private PasswordEncoder passwordEncoder;

    @PreAuthorize("hasAnyAuthority('admin.create')")
    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }

    @PreAuthorize("hasAnyAuthority('admin.create')")
    @RequestMapping("/save")
    public String save(Admin admin){
        //对密码进行加密处理
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));

        adminService.insert(admin);
        return "common/successPage";
    }

    @PreAuthorize("hasAnyAuthority('admin.edit')")
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PreAuthorize("hasAnyAuthority('admin.edit')")
    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(Model model,@PathVariable Long id){
        model.addAttribute("id",id);
        return "admin/upload";
    }

    @RequestMapping("/upload/{id}")
    public String update(@PathVariable Long id,@RequestParam("file") MultipartFile file) throws IOException {
        //获取uuid
        String newFileName = UUID.randomUUID().toString();

        //上传图片
        QiniuUtil.upload2Qiniu(file.getBytes(),newFileName);
        String url = "http://riiaaev13.hb-bkt.clouddn.com/" + newFileName;

        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl(url);

        //更新数据库
        adminService.update(admin);

        return "common/successPage";
    }


    @Reference
    private RoleService roleService;

    /**
     * 跳到权限分配页面
     * @param adminId 根据adminId展示用户的权限
     * @param model 将adminId和权限信息存入到共享域中
     * @return
     */
    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable("id") Long adminId,Model model){
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId", adminId);
        return "admin/assignShow";
    }

    /**
     * 根据用户分配角色
     * @param adminId
     * @param roleIds
     * @return
     */

    @PreAuthorize("hasAnyAuthority('admin.assgin')")
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){

        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }

    /**
     * 获取当前的登录信息
     * @return
     */
    @RequestMapping("/getInfo")
    @ResponseBody //ajax请求
    public Object getInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return authentication.getPrincipal();
    }
}
